package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntitySysUser;
import com.loongtech.bi.entity.system.EntitySysUserRole;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysUserManager;
import com.loongtech.bi.manager.system.SysUserProjectRoleManager;
import com.loongtech.bi.support.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
	
/**
 * 系统用户管理
 * 
 * @author xckuang
 * @date 2014-10-06
 */
@Controller("sysUserManagerAction")
@Scope("view")
public class SysUserManagerAction implements Serializable {
	private static final long serialVersionUID = 1L;
	@Autowired
	private Session session;
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private SysUserManager sysUserManager;
	@Autowired
	private SysUserProjectRoleManager sysUserProjectRoleManager;
	private EntitySysUser user;
	private List<EntitySysUser> userList = new ArrayList<EntitySysUser>();

	@Autowired
	public void init() {
		setUser(new EntitySysUser());
	}

	private boolean empty(String param) {
		return null == param || param.trim().isEmpty();
	}

	public void search() {
		List<Object> params = new ArrayList<Object>();
		if (user.getId() != null && user.getId() > 0) {
			params.add(EntitySysUser.K.id);
			params.add(user.getId());
		}
		if (!empty(user.getUsername())) {
			params.add(EntitySysUser.K.username);
			params.add(user.getUsername());
		}
		if (!empty(user.getDepartment())) {
			params.add(EntitySysUser.K.department);
			params.add(user.getDepartment());
		}
		if (!empty(user.getDescription())) {
			params.add(EntitySysUser.K.description);
			params.add(user.getDescription());
		}
		userList.clear();
		if (params.isEmpty()) {
			userList.addAll(sysUserManager.findAll());
		} else {
			userList.addAll(sysUserManager.findBy(params.toArray(new Object[params.size()])));
		}
		sysLogManager.addLog(session.getUserName(), "用户查询成功!");
	}

	public void modify(EntitySysUser user) {
		FacesContext context = FacesContext.getCurrentInstance();
		EntitySysUser cur = sysUserManager.get(user.getId());
		if (cur != null) {
			sysUserManager.merge(user);
			sysLogManager.addLog(session.getUserName(), "用户信息修改成功!userId="+user.getId());
			context.addMessage(null, new FacesMessage("用户信息修改成功!", "id="+user.getId()));
			search();
		}else{
			sysLogManager.addLog(session.getUserName(), "用户信息修改失败,不存在userId="+user.getId());
			context.addMessage(null, new FacesMessage("用户信息修改失败!", "不存在用户id="+user.getId()));
		}
	}

	public void delete(int userId) {
		FacesContext context = FacesContext.getCurrentInstance();
		EntitySysUser cur = sysUserManager.get(userId);
		if(null != cur && cur.getIsAdmin()){
			context.addMessage(null, new FacesMessage("用户删除失败!", "管理员不允许被删除!"));
			return;
		}
		if(null != cur && cur.getUsername().equals(session.getUserName())){
			context.addMessage(null, new FacesMessage("用户删除失败!", "不允许删除自己!"));
			return;
		}
		List<EntitySysUserRole> list = sysUserProjectRoleManager.findBy(EntitySysUserRole.K.userId,userId);
		if(null != list && !list.isEmpty()){
			context.addMessage(null, new FacesMessage("用户删除失败!", "用户已经分配给角色!"));
			return;
		}
		if(null != cur){
			sysUserManager.removeById(userId);
			sysLogManager.addLog(session.getUserName(), "用户删除成功!userId="+userId);
			context.addMessage(null, new FacesMessage("用户删除成功!", "用户id="+userId));
			search();
		}		
	}

	public void reset(int userId) {
		FacesContext context = FacesContext.getCurrentInstance();
		EntitySysUser cur = sysUserManager.get(userId);
		if(null != cur && cur.getIsAdmin()){
			context.addMessage(null, new FacesMessage("用户重置密码失败!", "管理员不允许被重置密码!"));
			return;
		}
		if(null != cur && cur.getUsername().equals(session.getUserName())){
			context.addMessage(null, new FacesMessage("用户重置密码失败!", "不允许重置自己的密码!"));
			return;
		}
		if (null != cur && sysUserManager.resetPassword(userId)) {
			sysLogManager.addLog(session.getUserName(), "用户重置密码成功!userId="+userId);
			context.addMessage(null, new FacesMessage("用户重置密码成功!", "用户id="+userId));
			search();
		}else{
			sysLogManager.addLog(session.getUserName(), "用户重置密码失败!userId="+userId);
			context.addMessage(null, new FacesMessage("用户重置密码失败!", "userId=="+userId));
		}
	}

	public List<EntitySysUser> getUserList() {
		return userList;
	}

	public void setUserList(List<EntitySysUser> userList) {
		this.userList = userList;
	}

	public EntitySysUser getUser() {
		return user;
	}

	public void setUser(EntitySysUser user) {
		this.user = user;
	}
}